<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use cmf\controller\HomeBaseController;
use think\Validate;
use app\user\model\UserModel;
use app\common\model\ShopUser;
class RegisterController extends HomeBaseController
{

    /**
     * 前台用户注册
     */
    public function index()
    {
        $objUsers = new ShopUser();
        $redirect = $this->request->post("redirect");
        if (empty($redirect)) {
            $redirect = $this->request->server('HTTP_REFERER');
        } else {
            $redirect = base64_decode($redirect);
        }
        session('login_http_referer', $redirect);

        if (cmf_is_user_login()) {
            return redirect($this->request->root() . '/');
        } else {
            return $this->fetch(":register");
        }
    }

    /**
     * 前台用户注册提交
     */
    public function doRegister()
    {
        $objUsers = new ShopUser();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!cmf_captcha_check($data['captcha'])) {
                $this->error('验证码错误');
            }
            $mobile = $_POST['mobile'];
            $password = md5($_POST['password']);
            if(empty($password)){
                $this->error('密码不能为空');
            }
            if(empty($mobile)){
                $this->error('手机号不能为空');
            }
            $data = array(
                'mobile' =>$mobile,
                'password' =>$password,
                'reg_time' =>time(),
                'user_status' => 1,
                'nickname' =>$mobile
            );
            $userinfo = $objUsers->where(array('mobile'=>$mobile))->field('mobile')->find();
            if($userinfo['mobile'] == $mobile){
                $this->error('该手机号已被注册，请更换手机号重试');
                return false;
            }
            $useradd = $objUsers->insert($data);
            if($useradd){
                $this->success('注册成功','/index.php/user/login/index.html');
            }else{
                $this->error('网络异常，请稍后重试');
            }
        } else {
            $this->error("请求错误");
        }

    }
}